package com.ruoyi.application.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.application.domain.TbMatchmakerApplication;
import com.ruoyi.application.service.ITbMatchmakerApplicationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import com.ruoyi.common.utils.SecurityUtils;

/**
 * 红娘服务申请Controller
 * 
 * @author ruoyi
 * @date 2025-07-31
 */
@RestController
@RequestMapping("/application/application")
public class TbMatchmakerApplicationController extends BaseController
{
    @Autowired
    private ITbMatchmakerApplicationService tbMatchmakerApplicationService;

    /**
     * 查询红娘服务申请列表
     */
    @PreAuthorize("@ss.hasPermi('application:application:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbMatchmakerApplication tbMatchmakerApplication)
    {
        startPage();
        List<TbMatchmakerApplication> list = tbMatchmakerApplicationService.selectTbMatchmakerApplicationList(tbMatchmakerApplication);
        return getDataTable(list);
    }

    /**
     * 导出红娘服务申请列表
     */
    @PreAuthorize("@ss.hasPermi('application:application:export')")
    @Log(title = "红娘服务申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbMatchmakerApplication tbMatchmakerApplication)
    {
        List<TbMatchmakerApplication> list = tbMatchmakerApplicationService.selectTbMatchmakerApplicationList(tbMatchmakerApplication);
        ExcelUtil<TbMatchmakerApplication> util = new ExcelUtil<TbMatchmakerApplication>(TbMatchmakerApplication.class);
        util.exportExcel(response, list, "红娘服务申请数据");
    }

    /**
     * 获取红娘服务申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('application:application:query')")
    @GetMapping(value = "/{applicationId}")
    public AjaxResult getInfo(@PathVariable("applicationId") Long applicationId)
    {
        return success(tbMatchmakerApplicationService.selectTbMatchmakerApplicationByApplicationId(applicationId));
    }



    /**
     * 审核红娘服务申请
     */
    @PreAuthorize("@ss.hasPermi('application:application:review')")
    @Log(title = "红娘服务申请审核", businessType = BusinessType.UPDATE)
    @PutMapping("/review")
    public AjaxResult review(@RequestBody TbMatchmakerApplication application)
    {
        Long reviewerId = SecurityUtils.getUserId();
        return toAjax(tbMatchmakerApplicationService.reviewApplication(
            application.getApplicationId(),
            application.getApplicationStatus(),
            application.getRejectReason(),
            reviewerId));
    }

    /**
     * 批量审核通过红娘服务申请
     */
    @PreAuthorize("@ss.hasPermi('application:application:review')")
    @Log(title = "批量审核通过红娘服务申请", businessType = BusinessType.UPDATE)
    @PutMapping("/batchApprove/{applicationIds}")
    public AjaxResult batchApprove(@PathVariable Long[] applicationIds)
    {
        Long reviewerId = SecurityUtils.getUserId();
        int successCount = 0;
        for (Long applicationId : applicationIds) {
            successCount += tbMatchmakerApplicationService.reviewApplication(applicationId, 1, null, reviewerId);
        }
        return toAjax(successCount);
    }

    /**
     * 批量审核拒绝红娘服务申请
     */
    @PreAuthorize("@ss.hasPermi('application:application:review')")
    @Log(title = "批量审核拒绝红娘服务申请", businessType = BusinessType.UPDATE)
    @PutMapping("/batchReject/{applicationIds}")
    public AjaxResult batchReject(@PathVariable Long[] applicationIds)
    {
        Long reviewerId = SecurityUtils.getUserId();
        String rejectReason = "批量拒绝";
        int successCount = 0;
        for (Long applicationId : applicationIds) {
            successCount += tbMatchmakerApplicationService.reviewApplication(applicationId, 2, rejectReason, reviewerId);
        }
        return toAjax(successCount);
    }

    /**
     * 删除红娘服务申请
     */
    @PreAuthorize("@ss.hasPermi('application:application:remove')")
    @Log(title = "红娘服务申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{applicationId}")
    public AjaxResult remove(@PathVariable Long applicationId)
    {
        return toAjax(tbMatchmakerApplicationService.deleteTbMatchmakerApplicationByApplicationId(applicationId));
    }


}
